<?php

/**
 * invitation actions.
 *
 * @package    nfzz
 * @subpackage invitation
 * @author     Francisco Calderón <fjcalderon@gmail.com>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
ProjectConfiguration::registerContacts();
ProjectConfiguration::registerTemplate();

class invitationActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->handler = new ContactsHandler();
    $this->handler->handle_request($_POST);
    if($this->handler->contactsPost){
	    $replacements = array();
	    
	    $mailer = $this->getMailer();
	    
      $message = Swift_Message::newInstance('NearFriendzz invitation');
      $message->setFrom(array('invite@nearfriendzz.com' => sfConfig::get('app_default_email_name')));
      $message->setTo($this->handler->contactsPost);
      
      // embeding files in to the message and obtain the image path
      $toplogoblue = $message->embed(Swift_Image::fromPath(sfConfig::get('sf_web_dir').'/images/email/toplogoblue.jpg'));
      $leftshadowblue = $message->embed(Swift_Image::fromPath(sfConfig::get('sf_web_dir').'/images/email/leftshadowblue.jpg'));
      $rightshadowblue = $message->embed(Swift_Image::fromPath(sfConfig::get('sf_web_dir').'/images/email/rightshadowblue.jpg'));
      $downblue = $message->embed(Swift_Image::fromPath(sfConfig::get('sf_web_dir').'/images/email/downblue.jpg'));
      $pie = $message->embed(Swift_Image::fromPath(sfConfig::get('sf_web_dir').'/images/email/pie.jpg'));

			foreach ($this->handler->contactsPost as $contact_email => $contact_name)
			{
        $replacements[$contact_email] = array(
          '{name}' => $contact_name,
          '{toplogoblue}' => $toplogoblue,
          '{leftshadowblue}' => $leftshadowblue,
          '{rightshadowblue}' => $rightshadowblue,
          '{downblue}' => $downblue,
          '{pie}' => $pie
        );
			}
			
	    $decorator = new Swift_Plugins_DecoratorPlugin($replacements);
      $mailer->registerPlugin($decorator);
      $message->setBody($this->getMessageBody('invitation'), 'text/html');

      //Send the message
      $this->numSent = $mailer->batchSend($message);
    }
    $this->setTemplate($this->handler->current_form);
  }

  protected function getMessageBody($template)
  {
    $loader = new sfTemplateLoaderFilesystem(sfConfig::get('sf_app_template_dir').'/emails/%name%.php');
    $engine = new sfTemplateEngine($loader);
    return $engine->render($template);
  }
}
